package com.github.postsanf.yinian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.StealPhotosDateAdapter;
import com.github.postsanf.yinian.bean.StealPhotoDate;
import com.github.postsanf.yinian.bean.StealPhotoInfoList;
import com.github.postsanf.yinian.utils.StringUtils;

import org.json.JSONArray;

import java.util.ArrayList;

public class StealPhotosDateActivity extends BaseActivity implements View.OnClickListener {
    private TextView photodate_title;
    private ListView mListView;
    private String yourunseenPhotoStr;
    //    private String yourPhotoStr;
    private String yourUnickname;
    private ArrayList<StealPhotoDate> dates = new ArrayList<>();
    private StealPhotoInfoList stealPhotoInfoList;
    private int beUserid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stealphotodate);
        yourunseenPhotoStr = getIntent().getStringExtra("yourunseenPhotoStr");
//        yourPhotoStr = getIntent().getStringExtra("yourPhotoStr");
        yourUnickname = getIntent().getStringExtra("yourunickname");
        beUserid = getIntent().getIntExtra("beUserid", 0);
        stealPhotoInfoList = (StealPhotoInfoList) getIntent().getSerializableExtra("stealPhotoInfoList");
        if (!StringUtils.isEmpty(yourunseenPhotoStr)) {
            try {
                JSONArray jsonArray = new JSONArray(yourunseenPhotoStr);
                for (int i = 0; i < jsonArray.length(); i++) {
                    StealPhotoDate stealPhotoDate = new StealPhotoDate();
                    stealPhotoDate.setNum(jsonArray.optJSONObject(i).optInt("num"));
                    stealPhotoDate.setYear(jsonArray.optJSONObject(i).optString("year"));
                    dates.add(stealPhotoDate);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        initView();
    }

    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        photodate_title = (TextView) findViewById(R.id.photodate_title);
        photodate_title.setText(yourUnickname + "的本地图片");
        mListView = (ListView) findViewById(R.id.photodate_lv);
        StealPhotosDateAdapter stealPhotosDateAdapter = new StealPhotosDateAdapter(StealPhotosDateActivity.this, dates);
        mListView.setAdapter(stealPhotosDateAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(StealPhotosDateActivity.this, StealPhotosYearActivity.class);
                intent.putExtra("num", dates.get(position).getNum());
                intent.putExtra("year", dates.get(position).getYear());
                intent.putExtra("beUserid", beUserid);
                intent.putExtra("stealPhotoInfoList", stealPhotoInfoList);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

}
